package ru.vasilij.selenium.applogic1;

import static org.openqa.selenium.support.ui.ExpectedConditions.presenceOfElementLocated;

import java.util.ArrayList;
import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.support.ui.ExpectedConditions;

import ru.vasilij.selenium.applogic.FilmHelper;
import ru.vasilij.selenium.model.Film;
import ru.vasilij.selenium.pages.FilmPage;


public class FilmHelper1 extends DriverBasedHelper implements FilmHelper {
	
	public FilmHelper1(ApplicationManager1 manager) {
	    super(manager.getWebDriver());
	}

	@Override
	public void create(Film film) {
		pages.internalPage.ensurePageLoaded()
			.clickAddFilmPage()
				.setnameField(film.getTitle())
				.setyearField(film.getYear())
				.clickSubmitButton();
	}
	
	@Override
	public boolean isCreateFilm() {
		return pages.filmPage.waitPageLoaded();
	}
	
	@Override
	public boolean isCreateFilmAs(Film film) {
		return isCreateFilm()
			&& getNameCreateFilm().equals(film.getTitleAndYear());
	}
	
	private String getNameCreateFilm() {
		FilmPage filmPage = pages.filmPage.ensurePageLoaded();
		return filmPage.getFilmNameAndYear();
	}
	
	@Override
	public boolean isNotCreateFilm() {
		return pages.filmAddPage.isErrorFiled();
	}
	
	@Override
	public Film showFirstFilm() {
		pages.internalPage.ensurePageLoaded()
			.waitLoadBlockFilms()
			.clickFirstFilm();
		return new Film()
			.setId(pages.filmPage.getId());
	}

	@Override
	public void delete(Film film) {
	    // TODO Auto-generated method stub
	}
	
	@Override
	public void delete() {
		pages.filmPage.ensurePageLoaded()
			.clickFilmDelete();
	}
	
	@Override
	public boolean isDeleteFilm() {
		return pages.internalPage.waitPageLoaded() 
			&& !pages.filmPage.waitPageLoaded();
	}

	@Override
	public List<Film> search(String title) throws InterruptedException {
		List<Film> LF = pages.internalPage.ensurePageLoaded()
			.waitLoadBlockFilms()
			.searchFilm(title);
			
	    return LF;
	}
	
	public void clearSearchField() throws InterruptedException {
		pages.internalPage.ensurePageLoaded()
			.waitLoadBlockFilms()
			.clearSearchField();
	}
	
}
